package com.blackteachan.zlb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.blackteachan.zlb.bean.ZlbUser;
import com.blackteachan.zlb.bean.ZlbUserToken;
import com.blackteachan.zlb.common.utils.Md5Util;
import com.blackteachan.zlb.service.ZlbUserService;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 浙里办用户接口实现类
 * @author blackteachan
 * @since 2021-11-25 10:19
 */
public class ZlbUserServiceImpl implements ZlbUserService {

    /**
     * 接口-票据认证
     */
    private static final String API_TICKET_VALIDATION = "https://appapi.zjzwfw.gov.cn/sso/servlet/simpleauth?method=ticketValidation";
    /**
     * 接口-根据临牌获取用户详细信息
     */
    private static final String API_GET_USER_INFO = "https://appapi.zjzwfw.gov.cn/sso/servlet/simpleauth?method=getUserInfo";


    /**
     * 浙里办接入码
     */
    private final String zlbServiceCode;

    /**
     * 浙里办接入密码
     */
    private final String zlbServicePwd;

    public ZlbUserServiceImpl(){
        ResourceBundle resource = ResourceBundle.getBundle("zlb");
        zlbServiceCode = resource.getString("servicecode");
        zlbServicePwd = resource.getString("servicepwd");
    }

    @Override
    public ZlbUserToken ticketValidation(String ticket) {

        //时间戳
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = simpleDateFormat.format(new Date());
        //MD5
        String sign = Md5Util.toHex((zlbServiceCode + zlbServicePwd + time).getBytes());
        //接口参数返回格式
        String datatype = "json";

        //接入资源验证临时票据的有效性
        String ticketValidationUrl = API_TICKET_VALIDATION
                + "&servicecode=" + zlbServiceCode
                + "&time=" + time
                + "&sign=" + sign
                + "&st=" + ticket
                + "&datatype=" + datatype;
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(ticketValidationUrl)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            assert response.body() != null;
            String body = response.body().string();
            System.out.printf("浙里办校验票据接口: %s\n", body);
            return JSONObject.parseObject(body, ZlbUserToken.class);
        } catch (IOException e) {
            System.out.printf("请求浙里办校验票据接口出错: %s\n", e.getMessage());
            throw new IllegalArgumentException("请求浙里办校验票据接口出错");
        }
    }

    @Override
    public ZlbUser getUserInfo(String token) {
        //时间戳
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = simpleDateFormat.format(new Date());
        //MD5
        String sign = Md5Util.toHex((zlbServiceCode + zlbServicePwd + time).getBytes());
        //接口参数返回格式
        String datatype = "json";

        // 获取用户信息
        String getUserInfoUrl = API_GET_USER_INFO
                + "&servicecode=" + zlbServiceCode
                + "&time=" + time
                + "&sign=" + sign
                + "&token=" + token
                + "&datatype=" + datatype;
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(getUserInfoUrl)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            assert response.body() != null;
            String body = response.body().string();
            System.out.printf("浙里办获取个人用户信息接口: %s\n", body);
            return JSONObject.parseObject(body, ZlbUser.class);
        } catch (IOException e) {
            System.out.printf("请求浙里办获取个人用户信息接口出错: %s\n", e.getMessage());
            throw new IllegalArgumentException("请求浙里办获取个人用户信息接口出错");
        }
    }
}
